<?php
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2015/11/17
 * Time: 13:57
 * 接收到数据后将使用线路做健，ON/OFF做值来存储
 */
class Filter {
    protected $fileArray='';
    protected $switchArray='';
    public function __construct($fileArray,$switchArray){
        $this->fileArray=$fileArray;
        $this->switchArray=$switchArray;
    }
    protected  function filterArray($key){
        $jsonKeys=array_keys($this->fileArray);
        foreach($jsonKeys as $jk=>$jv){
            foreach($this->fileArray[$jv] as $k=>$v){
                if($this->fileArray[$jv][$k]=='NC' && $k!=$key){
                    unset($this->fileArray[$jv][$k]);
                }
            }
        }
    }
    //打开文件
    protected function Opens($fileName){
        $this->fileObject=fopen($fileName,"r+");
    }
    static public function Get($fileName,$jsonObject){
        $file=JsonAction::Decode(FileAction::Read($fileName));//打开json文件，转换成数组
        $switch=SwitchType::Actions(JsonAction::Decode($jsonObject));//根据参数设置线路板状态
        switch($switch['switch']){
            case 'floors':
                $fileClass=new Floor($file,$switch);
                break;
            case 'alones':
                $fileClass=new Alone($file,$switch);
                break;
            case 'alls':
                $fileClass=new All($file,$switch);
                break;
            default:
                $fileClass=new Others($file,$switch);
        }
        FileAction::Write($fileName,$fileClass->To());
        return $fileClass->switchArray;
    }
}
//公寓楼层
class Floor extends Filter{
    public function __construct($fileArray,$switchArray){
        parent::__construct($fileArray,$switchArray);
    }
    public function To(){
        unset($this->switchArray['switch']);
        $switchKeys=array_keys($this->switchArray);
        $floorKeys=array_keys($this->fileArray['floors']);
        foreach($switchKeys as $sk=>$sv){
            if($this->fileArray['alones'][$sv]=='NO'){
                unset($this->switchArray[$sv]);//在独立户型中已开启的，但是现在要关闭的删除
                continue;
            }
            foreach($floorKeys as $fk=>$fv){
                if($this->fileArray['alones'][$fv]=='NO'){
                    unset($this->fileArray['floors'][$fv]);//在独立户型中已开启的，但是现在要关闭的删除
                }
                if($this->fileArray['floors'][$fv]=='NC' && !in_array($fv,$switchKeys)){
                    unset($this->fileArray['floors'][$fv]);//删除上次执行off，本次没有执行on的值
                }
            }
            $this->fileArray['floors'][$sv]=$this->switchArray[$sv];
        }
        return $this->fileArray;
    }
}
//独立户型
class Alone extends Filter{
    public function __construct($fileArray,$switchArray){
        parent::__construct($fileArray,$switchArray);
    }
    public function To(){
        unset($this->switchArray['switch']);
        $key=key($this->switchArray);
        $this->fileArray['alones'][$key]=$this->switchArray[$key];//添加或修改
        $this->filterArray($key);
        return $this->fileArray;
    }
}
//写字楼、避难层、会所
class Others extends Filter{
    public function __construct($fileArray,$switchArray){
        parent::__construct($fileArray,$switchArray);
    }
    public function To(){
        $jsonKey=$this->switchArray['switch'];
        unset($this->switchArray['switch']);
        $key=key($this->switchArray);
        if(empty($this->fileArray[$jsonKey])){//检测是否存在此对象，不存在就添加
            $this->fileArray[$jsonKey]='';
        }
        $this->fileArray[$jsonKey][$key]=$this->switchArray[$key];//添加或修改
        $this->filterArray($key);
        return $this->fileArray;
    }
}
//全部
class All extends Filter{
    public function __construct($fileArray,$switchArray){
        parent::__construct($fileArray,$switchArray);
    }
    public function To(){
        unset($this->switchArray['switch']);
        $jsonKeys=array_keys($this->fileArray);
        foreach($jsonKeys as $jk=>$jv){
            foreach($this->fileArray[$jv] as $k=>$v){
                unset($this->fileArray[$jv][$k]);
            }
        }
        return $this->fileArray;
    }
}
?>